package com.maistars.spider.service.domain.task;

import com.maistars.spider.service.capture.CaptureEngine;
import com.maistars.spider.service.domain.rule.SiteRule;
import com.maistars.spider.service.domain.store.TaskStoreConfig;
import com.maistars.spider.service.domain.rule.FieldRule;
import com.maistars.spider.service.domain.rule.PageRule;
import com.maistars.spider.service.extract.ExtractEngine;
import com.maistars.spider.service.render.PageRender;
import com.maistars.spider.service.store.TaskInstResultStore;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 爬虫任务实例
 * 阶段：页面URL捕捉阶段->字段解析阶段
 *
 * @author dhr
 * @date 2021/4/11 下午10:41
 */
@Builder
@Data
public class CrawlerTaskInst {

    private Integer taskInstId;

    /**
     * 爬虫任务定义
     */
    private CrawlerTask crawlerTask;

    /**
     * 开始时间
     */
    private Date startTime;

    /**
     * 结束时间
     */
    private Date endTime;

    /**
     * 当前任务实例所处状态
     * 1.任务初始化
     * 2.页面URL捕捉
     * 3.每页最终字段解析
     */
    private Integer status;

    /**
     * k:层级 v:数量
     */
    private Map<Integer, AtomicInteger> capturedPageTotoalCount = new HashMap<>();

    /**
     * k:层级 v:数量
     */
    private Map<Integer, AtomicInteger> capturedPageSuccessCount = new HashMap<>();

    private Integer capturedPageThreadNum;

    private Integer extractedFieldThreadNam;

    private Integer capturedPageTimeout;

    private Integer extractedFieldTimeout;

    /**
     * 每层最大捕捉url数量
     */
    private Map<Integer, Integer> maxCaptureUrlNums = new HashMap<>();

    private CaptureEngine captureEngine;

    private ExtractEngine extractEngine;

    private PageRender pageRender;

    /**
     * 任务存储配置
     */
    @Getter
    private TaskStoreConfig taskStoreConfig;

    private TaskInstResultStore taskInstResultStore;

    public CaptureEngine getCaptureEngine(SiteRule siteRule, List<PageRule> pageRules) {
        if (captureEngine == null) {
            captureEngine = new CaptureEngine(this, siteRule, pageRules, capturedPageThreadNum == null ? 1 : capturedPageThreadNum);
        }
        return captureEngine;
    }

    public ExtractEngine getExtractEngine(PageRule pageRule) {
        if (extractEngine == null) {
            extractEngine = new ExtractEngine(this,pageRule, extractedFieldThreadNam == null ? 1 : extractedFieldThreadNam);
        }
        return extractEngine;
    }

    public TaskInstResultStore getTaskInstResultStore() {
        if (taskInstResultStore == null) {
            synchronized (this) {
                if (taskInstResultStore == null) {
                    taskInstResultStore = new TaskInstResultStore(this);
                    taskInstResultStore.init();
                }
            }
        }
        return taskInstResultStore;
    }


}
